/**
 * 
 */
package comint03.evaluator;

/**
 * @comint01_trunk, comint01.evaluator
 * @author dna
 */
public class Environment {
	
	private Environment enclosing;
	private Frame frame;
	
	/**
	 * @param enclosing
	 * @param frame
	 */
	public Environment(Environment enclosing, Frame frame) {
		this.enclosing = enclosing;
		this.frame = frame;
	}
	
	public Environment(){
		this.enclosing = null;
		this.frame = null;
	}

	public Frame getFrame(){
		return this.frame;
	}
	
	public Value get(Ident id){
		Value v = frame.get(id);
		if (v == null){
			if (enclosing == null)
				throw new RuntimeException("Undefined id.");
			
			v =  enclosing.get(id);
		}
		return v;
	}
	
	public void set(Ident id, Value value){
		Value v = frame.get(id);
		if (v == null)
			enclosing.set(id,value);
		frame.set(id, value);
	}
}
